
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/snack_bar.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

import '../models/bottom_sheet.dart';

class BottomSheetDialog {

  BottomSheetDialog._();

  static var _itemWidth = 0.0;
  static TextEditingController controllers = TextEditingController();

  static showMenuByNames(BuildContext ctx, List<String> options) async {
    if (options.isEmpty) return;
    final list = <BSItem>[];
    for (var element in options) {
      list.add(BSItem(name: element));
    }
    return showMenu(ctx, list);
  }

  static showMenu(BuildContext context, List<BSItem> options) async {
    final items = <Widget>[];
    final screenSize = MediaQuery.of(context).size;
    _itemWidth = screenSize.width;
    double dialogMaxHeight = screenSize.height * 0.8;
    double dialogHeight = (options.length + 1) * 53 + 20;
    for (int i = 0; i < options.length; i ++) {
      items.add(_buildItem(context, options[i].name, options[i].color, i));
      if (i < options.length - 1) {
        items.add(Divider(color:ThemeColor.dividerColor, height: 1,));
      }
    }
    items.add(Divider(color: ThemeColor.dividerColor, height: 20.0,));
    items.add(_buildItem(context, "取消", 0xFF222222, -1));
    return showModalBottomSheet<int>(
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return Container(
          height: dialogHeight > dialogMaxHeight ? dialogMaxHeight : dialogHeight,
          clipBehavior: Clip.antiAlias,
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.0),
              topRight: Radius.circular(12.0),
            ),
          ),
          child: SingleChildScrollView (
            dragStartBehavior: DragStartBehavior.down,
            physics: const BouncingScrollPhysics(),
            child: Column(children: items),
          )
        );
      },
    );
  }

  static Widget _buildItem(BuildContext context, String name, int textColor, int index) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pop(index);
      },
      child: Container(
        height: 52,
        width: _itemWidth,
        color: Colors.transparent,
        // decoration: BoxDecoration(
        //   border: Border(
        //       bottom: BorderSide(
        //           color: ThemeColor.dividerColor,
        //           width: 1
        //       )
        //   ),
        // ),
        alignment: Alignment.center,
        child: Text(
          name,
          style: TextStyle(
              color: Color(textColor),
              fontSize: 15
          ),
        ),
      ),
    );
  }

   static  edit(BuildContext context) async {
    return showModalBottomSheet<String>(
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      context: context,
      builder: (ctx) {
        return AnimatedPadding(
          padding: EdgeInsets.only(
            bottom: MediaQuery.of(context).viewInsets.bottom
          ),
          duration: Duration.zero,
          child: Container(
            clipBehavior: Clip.antiAlias,
            decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(12.0),
                    topRight: Radius.circular(12.0)
                )
            ),
            padding: const EdgeInsets.all(20),
            child: Container(
              // height: 120,
              height: 40,
              padding: const EdgeInsets.symmetric(horizontal: 12),
              decoration: ShapeDecoration(
                  color: const Color(0xFFF9F9F9),
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8))
              ),
              child: TextField(
                controller: controllers,
                maxLines: 5,
                style: TextStyle(
                    color: ThemeColor.textColor,
                    fontSize: 14
                ),
                decoration: InputDecoration(
                  border: InputBorder.none,
                  counterText: "",
                  hintText: "请输入",
                  hintStyle: TextStyle(
                      color: ThemeColor.textColorSecond,
                      fontSize: 14
                  ),
                  suffixIcon: GestureDetector(
                    onTap: (){
                      Navigator.of(context).pop(controllers.text);
                    },
                    child: Container(
                      padding: const EdgeInsets.only(top: 10),
                      child: Text(
                        '保存',
                        style: TextStyle(
                          color: ThemeColor.appColor
                        ),
                      ),
                    ),
                  ),
                ),
                textInputAction: TextInputAction.send,
                onSubmitted: (value) {
                  Navigator.of(context).pop(value);
                },
              ),
            ),
          ),
        );
      }
    );
  }

  static show(BuildContext context, Widget child) async {
    return showModalBottomSheet(
        context: context,
        backgroundColor: Colors.transparent,
        builder: (ctx) => child
    );
  }


}


